package com.etogc.groupchat.group.adpt;

import android.content.Context;
import android.text.Spannable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.etogc.groupchat.R;
import com.etogc.util.ImageUtils;
import com.etogc.util.TimeUtil;

import java.util.Date;

import io.rong.imkit.RongIM;
import io.rong.imkit.model.UIConversation;
import io.rong.imkit.widget.adapter.ConversationListAdapter;

/**
 * Created by McGrady on 2017/3/8.
 */

public class ConversationAdapter extends ConversationListAdapter {

    private Context mContext;

    public ConversationAdapter(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    protected View newView(Context context, int position, ViewGroup group) {
        View view = View.inflate(mContext, R.layout.conversation_item, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.photo = (ImageView) view.findViewById(R.id.photo);
        viewHolder.name = (TextView) view.findViewById(R.id.name);
        viewHolder.content = (TextView) view.findViewById(R.id.content);
        viewHolder.time = (TextView) view.findViewById(R.id.time);
        viewHolder.unread = (TextView) view.findViewById(R.id.unread);
        viewHolder.unreadImg = (ImageView) view.findViewById(R.id.unread_img);
        view.setTag(viewHolder);
        return view;
    }

    @Override
    protected void bindView(View v, int position, final UIConversation data) {
        ViewHolder holder = (ViewHolder) v.getTag();
        if (data != null) {
            holder.name.setText(data.getUIConversationTitle());

            final String conversationSenderId = data.getConversationSenderId();
            final String conversationTargetId = data.getConversationTargetId();

            holder.photo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RongIM.getInstance().startGroupChat(mContext, conversationTargetId, "标题");
                }
            });


            Spannable conversationContent = data.getConversationContent();
            holder.content.setText(conversationContent);

            int unReadMessageCount = data.getUnReadMessageCount();
            if (unReadMessageCount > 0) {
                holder.unread.setVisibility(View.VISIBLE);
                holder.unreadImg.setVisibility(View.VISIBLE);
                holder.unread.setText(data.getUnReadMessageCount() + "");
                if (unReadMessageCount >= 10) {
                    holder.unreadImg.setImageResource(R.drawable.unread_double);
                } else {
                    holder.unreadImg.setImageResource(R.drawable.unread);
                }
            } else {
                holder.unread.setVisibility(View.GONE);
                holder.unreadImg.setVisibility(View.GONE);
            }

            String timestampString = TimeUtil.getTimeFormatText(new Date(data.getUIConversationTime()));
            holder.time.setText(timestampString);
            ImageUtils.setUriRadiuImage(mContext, data.getIconUrl(), holder.photo);
        }
    }

    class ViewHolder {
        ImageView photo, unreadImg;
        TextView name, content, time, unread, officical;
    }
}
